package com.tosee.logic.impl;

import java.util.Date;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.tosee.exception.GeneriqueException;
import com.tosee.logic.AbstractBusinessLogic;
import com.tosee.logic.IUserBusinessLogic;
import com.tosee.model.dbTosee.Episode;
import com.tosee.model.dbTosee.Serie;
import com.tosee.model.dbTosee.User;
import com.tosee.model.dto.AbstractDto;
import com.tosee.model.dto.UserDto;
import com.tosee.utils.Utils;

/**
 * Classe BL user
 * 
 * @author RobinT
 */
@Service
public class UserBusinessLogic extends AbstractBusinessLogic implements IUserBusinessLogic {

    /**
     * LOGGER.
     */
    private static final Log LOGGER = LogFactory.getLog(UserBusinessLogic.class);

    @Override
    public UserDto authentifier(AbstractDto _abstractDto) {

        UserDto retour = null;
        if(_abstractDto != null && _abstractDto.getAuthentification() != null
                && StringUtils.isNotBlank(_abstractDto.getAuthentification().getLogin())
                && StringUtils.isNotBlank(_abstractDto.getAuthentification().getMdp())) {

            User user = getDaoProvider().getUserDao().getUserByLogin(_abstractDto.getAuthentification().getLogin());
            if(user != null && StringUtils.isNotBlank(user.getMdp()) && StringUtils.isNotBlank(user.getGrainSel())) {
                if(Utils.validerMdp(_abstractDto.getAuthentification().getMdp(), user.getMdp(), user.getGrainSel())) {
                    retour = new UserDto();
                    retour.setLogin(user.getLogin());
                    retour.setMdp(user.getMdp());
                    retour.setId(user.getId());
                    retour.setEmail(user.getEmail());
                    retour.setGds(user.getGrainSel());
                    retour.setDateCreation(user.getDateCreation());
                    retour.setDateLastAction(user.getDateLastAction());
                }
            }
        }
        return retour;
    }

    @Override
    public Integer creerUtilisateur(UserDto _user) throws GeneriqueException {

        Integer retour = null;

        // Controle
        getControleProvider().getUserControle().controlerUtilisateur(_user);

        if(_user != null) {
            User user = new User();
            user.setDateCreation(new Date());
            user.setDateLastAction(new Date());
            user.setEmail(_user.getEmail());
            user.setLogin(_user.getLogin());
            user.setGrainSel(RandomStringUtils.randomAlphanumeric(5));

            user.setMdp(Utils.encrypterPassword(_user.getMdp(), user.getGrainSel()));

            getDaoProvider().getUserDao().save(user);
            retour = user.getId();

        }

        return retour;
    }

    @Override
    public User getUser(AbstractDto _abstract) {
        User retour = null;

        if(_abstract != null && _abstract.getAuthentification() != null) {
            retour = getDaoProvider().getUserDao().getUserById(_abstract.getAuthentification().getId());
        }
        if(_abstract.getAuthentification().getId() == null && retour == null) {
            retour = getDaoProvider().getUserDao().getUserByLogin(_abstract.getAuthentification().getLogin());
        }

        return retour;
    }

    @Override
    public boolean ajouterSerieToUser(User _user, Serie _serieLocale) throws GeneriqueException {
        boolean retour = false;
        if(_user != null && _serieLocale != null) {
            if(CollectionUtils.isEmpty(_user.getListeSerie()) || !_user.getListeSerie().contains(_serieLocale)) {
                _user.getListeSerie().add(_serieLocale);
                getDaoProvider().getUserDao().update(_user);
            }
            retour = true;
        }
        return retour;
    }

    @Override
    public boolean ajouterEpisodeToUser(User _user, Episode _episodeLocal) throws GeneriqueException {
        boolean retour = false;
        if(_user != null && _episodeLocal != null) {
            if(CollectionUtils.isEmpty(_user.getListeEpisode()) || !_user.getListeEpisode().contains(_episodeLocal)) {
                _user.getListeEpisode().add(_episodeLocal);
                getDaoProvider().getUserDao().update(_user);
            }
            retour = true;
        }
        return retour;
    }
}
